from odoo import fields, models, api
from odoo.tools.float_utils import float_compare, float_is_zero
from odoo.exceptions import ValidationError


class PostedScrapWizard(models.TransientModel):
    _name = 's.posted.scrap.wizard'
    _description = 'Posted Scrap Wizard'

    workorder_id = fields.Many2one('mrp.workorder', string='Work Order', readonly=True)
    workcenter_id = fields.Many2one('mrp.workcenter', related='workorder_id.workcenter_id')
    production_id = fields.Many2one('mrp.production', string='Production', readonly=True, related='workorder_id.production_id')
    product_id = fields.Many2one('product.product', string='Product', readonly=True, related='workorder_id.product_id')
    order_qty = fields.Float('Order Quantity', readonly=True)
    order_set_qty = fields.Float('Order SET Quantity', readonly=True)
    order_panel_qty = fields.Float('Order PANEL Quantity', readonly=True)
    scrap_qty = fields.Float('Scrap Quantity', compute='_compute_quantity')
    scrap_set_qty = fields.Float('Scrap SET Quantity', compute='_compute_quantity')
    scrap_panel_qty = fields.Float('Scrap PANEL Quantity', compute='_compute_quantity')
    line_ids = fields.One2many('s.posted.scrap.line.wizard', 'order_id')

    @api.model
    def default_get(self, fields_list):
        res = super().default_get(fields_list)
        active_id = self._context.get('active_id')
        workorder_id = self.env['mrp.workorder'].browse(active_id)
        res.update({
            'workorder_id': workorder_id.id,
            'order_qty': workorder_id.qty_wip,
            'order_set_qty': workorder_id.qty_wip_set,
            'order_panel_qty': workorder_id.qty_wip_pnl,
        })
        return res

    @api.depends('line_ids')
    def _compute_quantity(self):
        for rec in self:
            rec.scrap_qty = sum(rec.line_ids.mapped('scrap_qty'))
            rec.scrap_set_qty = sum(rec.line_ids.mapped('scrap_set_qty'))
            rec.scrap_panel_qty = sum(rec.line_ids.mapped('scrap_panel_qty'))

    def action_confirm(self):
        self.ensure_one()
        if float_is_zero(self.scrap_panel_qty, precision_rounding=self.workorder_id.product_uom_id.rounding):
            raise ValidationError('The scrap quantity is zero!')
        if float_compare(self.scrap_panel_qty, self.order_panel_qty, precision_rounding=self.workorder_id.product_uom_id.rounding) == 1:
            raise ValidationError('Exceeding the scrap quantity!')
        vals_list = []
        for line in self.line_ids:
            vals_list.append({
                'workorder_id': self.workorder_id.id,
                'scrap_workorder_id': line.scrap_workorder_id.id,
                'scrap_reason_id': line.scrap_reason_id.id,
                'scrap_qty': line.scrap_qty,
                'scrap_set_qty': line.scrap_set_qty,
                'scrap_panel_qty': line.scrap_panel_qty
            })
        if vals_list:
            self.env['s.workorder.scrap.record'].create(vals_list)


class PostedScrapLineWizard(models.TransientModel):
    _name = 's.posted.scrap.line.wizard'
    _description = 'Posted Scrap line Wizard'

    order_id = fields.Many2one('s.posted.scrap.wizard')
    scrap_workorder_id = fields.Many2one('mrp.workorder', 'responsibility work')
    scrap_reason_id = fields.Many2one('scrap.reason', string="Scrap Reason",  required=True)
    scrap_qty = fields.Float('Scrap Quantity')
    scrap_set_qty = fields.Float('Scrap SET Quantity')
    scrap_panel_qty = fields.Float('Scrap PANEL Quantity')

    @api.onchange('scrap_panel_qty')
    def onchange_scrap_panel_qty(self):
        self.scrap_qty = self.scrap_panel_qty * self.order_id.product_id.value_pnl
        self.scrap_set_qty = self.scrap_qty / self.order_id.product_id.value_set
